import java.util.*;

class Solution1 {
    public int minInsertions(String s) {
        int n = s.length();
        int[][] dp = new int[n][n];
        for(int j = n-1; j>=0; j--) {
            for(int i = j; i<n; i++ ) {
                if(s.charAt(i) == s.charAt(j)) {
                    if(i == j || j+1 == i) {
                        dp[i][j] = 0;
                    }else {
                        dp[i][j] = dp[i+1][j-1];
                    }
                }else {
                    dp[i][j] = Math.min(dp[i+1][j] + 1, dp[i][j-1] + 1);
                }
            }
        }

        return dp[0][n-1];
    }
}

class Solution2 {
    public int longestCommonSubsequence(String s1, String s2) {
        int n = s1.length(), m = s2.length();
        int[][]dp = new int[n][m];
        for(int i = 1; i<=n; i++) {
            for(int j = 1; j<=m; j++) {
                if(s1.charAt(i-1) == s2.charAt(j-1)) {
                    dp[i][j] = dp[i-1][j-1] + 1;
                }else {
                    dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
                }
            }
        }

        return dp[n][m];
    }
}

class Solution3 {
    public List<String> buildArray(int[] target, int n) {
        List<String> list = new ArrayList<>();
        HashSet<Integer> set = new HashSet<>();
        for(int x : target) {
            set.add(x);
        }
        for(int i = 0; i<=n; i++) {
            if(set.contains(i)) {
                list.add("Push");
            }else {
                list.add("Push");
                list.add("Pop");
            }
        }

        return list;
    }
}

class Solution4 {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        int n = nums1.length, m = nums2.length;
        int[] ret = new int[n];
        for(int i = 0; i<n; i++) {
            for(int j = 0; j<m; j++) {
                if(nums1[i] == nums2[j]) {
                    for(int k = j+1; k<m; k++) {
                        if(nums2[k] > nums1[i]) {
                            ret[i] = nums2[k];
                            break;
                        }
                    }
                }
            }
            if(ret[i] == 0) ret[i] = -1;
        }
        return ret;
    }
}

class Solution {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        Stack<Integer> stack = new Stack<>();
        Map<Integer,Integer> map = new HashMap<>();
        int n = nums1.length, m = nums2.length;
        for(int i = m-1; i>=0; i--) {
            while (!stack.isEmpty() && stack.peek() < nums2[i]) {
                stack.pop();
            }
            if(stack.isEmpty()) map.put(nums2[i],-1);
            else map.put(nums2[i],stack.peek());
            stack.push(nums2[i]);
        }

        int[] ret = new int[n];
        for(int i = 0; i<n; i++) {
            ret[i] = map.get(nums1[i]);
        }

        return ret;
    }
}

public class Test {

}
